package com.choudou5.solr.bean.common;



import com.choudou5.solr.util.StrUtil;

import java.io.Serializable;

/**
 * @Name：SQL 查询Bean
 * @@author hsolr@hotmail.com
 * @@date 2018-01-13
 */
public class SqlQueryBean implements Serializable {

    private String where;
    private PageBean pageBean;
    private OrderBean orderBean;


    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public OrderBean getOrderBean() {
        return orderBean;
    }

    public void setOrderBean(OrderBean orderBean) {
        this.orderBean = orderBean;
    }

    public void setDefParam(int pageNo, int pageSize, String orderBy, String order) {
        setPageDefParam(pageNo, pageSize);
        setOrderDefParam(orderBy, order);
    }

    public void setOrderDefParam(String orderBy, String order) {
        setOrderParam(orderBy, order, true);
    }
    public void setOrderParam(String orderBy, String order) {
        setOrderParam(orderBy, order, false);
    }

    private void setOrderParam(String orderBy, String order, boolean isSetDef) {
        OrderBean orderBean = this.getOrderBean();
        if(orderBean == null) {
            setOrderBean(new OrderBean(orderBy, order));
        } else {
            if(!isSetDef){
                orderBean.setOrderBy(orderBy);
                orderBean.setOrder(StrUtil.isBlank(order) ? "DESC" : order);
            }
        }
    }

    public void setPageDefParam(int pageNo, int pageSize) {
        setPageParam(pageNo, pageSize, true);
    }
    public void setPageParam(int pageNo, int pageSize) {
        setPageParam(pageNo, pageSize, false);
    }

    private void setPageParam(int pageNo, int pageSize, boolean isSetDef) {
        PageBean pageBean = this.getPageBean();
        if(pageBean == null) {
            this.setPageBean(new PageBean(pageNo, pageSize));
        }else{
            if(!isSetDef){
                pageBean.setPageNo(pageNo);
                pageBean.setPageSize(pageSize);
            }
        }
    }

    public void setDefPage() {
        PageBean pageBean = this.getPageBean();
        if(pageBean == null) {
            this.setPageBean(new PageBean(1, PageBean.DEF_PAGE_SIZE));
        }
    }

    public String toLimitScript() {
        return " LIMIT " + pageBean.getStart() + ", " + pageBean.getPageSize();
    }

    public String getOrderSql() {
        return orderBean==null?"":orderBean.getOrderSql();
    }

}
